package com.jof.framework.jdbc;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
 * 支持数据源自动切换的数据源
 * @author Jonathan
 *
 */
public class HOPCombinationDataSource implements DataSource {
	private List<DataSource> dataSources = new ArrayList<DataSource>();
	private HOPJdbcConfiguration configuration;
	public List<DataSource> getDataSources() {
		return dataSources;
	}

	public void setDataSources(List<DataSource> dataSources) {
		this.dataSources = dataSources;
	}

	public HOPJdbcConfiguration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(HOPJdbcConfiguration configuration) {
		this.configuration = configuration;
	}

	
	public PrintWriter getLogWriter() throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			return dataSource.getLogWriter();
		}
		return null;
	}

	
	public void setLogWriter(PrintWriter out) throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			dataSource.setLogWriter(out);
		}
	}

	
	public void setLoginTimeout(int seconds) throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			dataSource.setLoginTimeout(seconds);
		}
	}

	
	public int getLoginTimeout() throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			return dataSource.getLoginTimeout();
		}
		return 0;
	}

	
	public <T> T unwrap(Class<T> iface) throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			return dataSource.unwrap(iface);
		}
		return null;
	}

	
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			return dataSource.isWrapperFor(iface);
		}
		return false;
	}

	
	public Connection getConnection() throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			Connection connection = dataSource.getConnection();
			return new HOPConnection(connection, configuration);
		}
		return null;
	}

	
	public Connection getConnection(String username, String password) throws SQLException {
		DataSource dataSource = getAvaiableDataSource();
		if(dataSource != null){
			Connection connection = dataSource.getConnection(username,password);
			return new HOPConnection(connection, configuration);
		}
		return null;
	}

	private DataSource getAvaiableDataSource(){
		for(DataSource dataSource : dataSources){
			if(isDataSourceAvaiable(dataSource)){
				return dataSource;
			}
		}
		return null;
	}
	
	/**
	 * 这里需要改动成判断数据源是否存活的判断逻辑
	 * @param dataSource
	 * @return
	 */
	private boolean isDataSourceAvaiable(DataSource dataSource){
		return true;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}
}
